package com.bnyy.medicalHousekeeper.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.gbp.customsView.ScaleImageView;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.PopupWindowHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.adapter.ShopDetailAdapter;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.bean.Shop;
import com.bnyy.medicalHousekeeper.bean.ShopDetail;
import com.bnyy.medicalHousekeeper.bean.Tag;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/9
 **/
public class ShopDetailActivity extends BaseActivityImpl {
    @BindView(R.id.iv_header)
    ScaleImageView ivHeader;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.tag_flow_layout)
    TagFlowLayout tagFlowLayout;
    @BindView(R.id.tv_year)
    TextView tvYear;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private ShopDetailAdapter adapter;
    private Shop shop;

    public static void show(Context context, Shop shop) {
        Intent intent = new Intent(context, ShopDetailActivity.class);
        intent.putExtra("shop", shop);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_detail;
    }

    @Override
    public String getTitleStr() {
        return "商家详情";
    }

    @Override
    public boolean unuseRefreshLayout() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        shop = (Shop) getIntent().getSerializableExtra("shop");

        TextView tv = new TextView(mContext);
        tv.setText("查看店铺");
        tv.setTextAppearance(R.style.NormalTextView_Small);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShopActivity.show(mContext, shop.getShop_id());
            }
        });
        setOptions(tv);

        GlideHelper.setCircleImage(mContext, shop.getShop_image(), ivHeader, R.mipmap.icon_default_header_man);

        tvName.setText(shop.getUser_name());
        tvPhone.setText(shop.getPhone());
        tvShopName.setText("店铺信息：" + shop.getShop_name());

        tagFlowLayout.setAdapter(new TagAdapter<Tag>(shop.getTags()) {
            int margin = SizeUtils.dp2px(5);

            @Override
            public View getView(FlowLayout parent, int position, Tag tag) {
                ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                lp.rightMargin = margin;

                TextView tv = new TextView(mContext);
                tv.setLayoutParams(lp);
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                tv.setPadding(10, 5, 10, 5);
                tv.setTextColor(Color.parseColor(tag.getTextColor()));
                GradientDrawable bg = new GradientDrawable();
                bg.setCornerRadius(5);
                bg.setColor(Color.parseColor(tag.getBackgroundColor()));
                tv.setBackground(bg);
                tv.setText(tag.getText());

                return tv;
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ShopDetailAdapter(mContext);
        recyclerView.setAdapter(adapter);

        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        tvYear.setText(year + "");
        tvYear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> options = new ArrayList<>();
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                for (int y = year; y >= 2019; y--) {
                    options.add(y + "");
                }
                PopupWindowHelper.getInstance(mContext).showSingleSelectPopupWindow(options, new PopupWindowHelper.SingleSelect.CallBackImpl() {
                    @Override
                    public void onSelect(String option, int position) {
                        super.onSelect(option, position);
                        if (!option.equals(tvYear.getText().toString())) {
                            tvYear.setText(option);
                            getShopDetail(Integer.parseInt(option));
                        }
                    }
                });
            }
        });
        getShopDetail(year);
    }

    private void getShopDetail(int year) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", shop.getId());
        params.put("year", year);

        requestManager.request(requestManager.mJavaRetrofitService.getShopDetail(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<ArrayList<ShopDetail>>(mContext) {
                    @Override
                    public void onSuccess(ArrayList<ShopDetail> shopDetails) {
                        super.onSuccess(shopDetails);
                        adapter.refresh(shopDetails);
                    }
                });
    }
}






























































